/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sabatec.controllers;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;

import br.com.sabatec.entities.RequestServiceSabatec;
import br.com.sabatec.service.RequestSabatecService;

/**
 * 
 * @author Daniel.Morais
 */
@ManagedBean(name = "requestServiceSabatecBean")
public class RequestServiceSabatecBean {

	private RequestServiceSabatec requestService = new RequestServiceSabatec();
	private List<RequestServiceSabatec> requestServicos;

	public List<RequestServiceSabatec> getRequestServicos() {
		return requestServicos;
	}

	public void setRequestServicos(List<RequestServiceSabatec> requestServicos) {
		this.requestServicos = requestServicos;
	}

	public RequestServiceSabatec getRequestService() {
		return requestService;
	}

	public void setRequestService(RequestServiceSabatec requestService) {
		this.requestService = requestService;
	}

	@PostConstruct
	public void init() {
		try {
			this.requestServicos = RequestSabatecService.listarTodos();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void salva(RequestServiceSabatec request) {
		FacesMessage msg = null;
		boolean loggedIn = false;
		RequestContext context = RequestContext.getCurrentInstance();
		try {
			loggedIn = true;
			RequestSabatecService.salva(request);
			msg = new FacesMessage(FacesMessage.SEVERITY_INFO,
					"Cadastro realizado com sucesso", "");
			this.requestServicos = RequestSabatecService.listarTodos();
		} catch (Exception e) {
			e.printStackTrace();
			loggedIn = false;
			msg = new FacesMessage(FacesMessage.SEVERITY_ERROR,
					"Problema ao realizar o cadastro", "");
		}
		FacesContext.getCurrentInstance().addMessage(null, msg);
		context.addCallbackParam("loggedIn", loggedIn);
	}

	public void salvaUpdate(RequestServiceSabatec request) {

		RequestContext context = RequestContext.getCurrentInstance();
		FacesMessage msg;
		boolean loggedIn;

		try {
			loggedIn = true;
			RequestSabatecService.salvaUpdate(request);
			msg = new FacesMessage(FacesMessage.SEVERITY_INFO,
					"Cadastro realizado com sucesso", "");
			this.requestServicos = RequestSabatecService.listarTodos();
			showDialogClose();
		} catch (Exception e) {
			e.printStackTrace();
			loggedIn = false;
			msg = new FacesMessage(FacesMessage.SEVERITY_ERROR,
					"Problema ao realizar o cadastro", "");
			showDialogClose();
		}
		FacesContext.getCurrentInstance().addMessage(null, msg);
		context.addCallbackParam("loggedIn", loggedIn);
	}

	public void exclui(RequestServiceSabatec request) {

		FacesMessage msg = null;
		boolean loggedIn = false;
		RequestContext context = RequestContext.getCurrentInstance();
		try {
			loggedIn = true;
			RequestSabatecService.exclui(request);
			msg = new FacesMessage(FacesMessage.SEVERITY_INFO,
					"Registro excluido com sucesso", "");
			this.requestServicos = RequestSabatecService.listarTodos();
		} catch (Exception e) {
			e.printStackTrace();
			loggedIn = false;
			msg = new FacesMessage(FacesMessage.SEVERITY_ERROR,
					"Problema ao excluir registro", "");
		}
		FacesContext.getCurrentInstance().addMessage(null, msg);
		context.addCallbackParam("loggedIn", loggedIn);

	}

	public void showDialog() {
		RequestContext.getCurrentInstance().execute("dlg2.show()");
	}
	
	public void showDialogClose() {
		RequestContext.getCurrentInstance().execute("dlg2.hide()");
	}
	


}
